package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.entity.TopMV;

import java.util.List;


import jp.wasabeef.glide.transformations.BlurTransformation;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

public class MVAdapter extends RecyclerView.Adapter<MVAdapter.MVViewHolder> {
    private List<TopMV> mvList;
    private Context context;
    private BaseOnclickListener onclickListener;

    public void setData(List<TopMV> mvList){
        this.mvList = mvList;
        notifyDataSetChanged();
    }

    public MVAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public MVViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MVViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mv, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MVViewHolder holder, int position) {
        TopMV mv = mvList.get(position);

        Glide.with(context)
                .load(mv.getCover())
                .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3)))
                .into(holder.imgBg);

        Glide.with(context)
                .load(mv.getCover())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))
                .into(holder.imgMV);

        holder.tvTittle.setText(mv.getName());
        holder.tvCount.setText(mv.getPlayCount());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onclickListener != null){
                    onclickListener.click(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mvList == null ? 0 : mvList.size();
    }

    class MVViewHolder extends RecyclerView.ViewHolder{
        View view;
        ImageView imgBg;
        ImageView imgMV;
        TextView tvTittle;
        TextView tvCount;

        public MVViewHolder(@NonNull View itemView) {
            super(itemView);

            view = itemView.findViewById(R.id.view_layout);
            imgBg = itemView.findViewById(R.id.background);
            imgMV = itemView.findViewById(R.id.mv);
            tvTittle = itemView.findViewById(R.id.tv_tittle);
            tvCount = itemView.findViewById(R.id.play_count);
        }
    }

    public void setBaseOnclickListener(BaseOnclickListener onclickListener){
        this.onclickListener = onclickListener;
    }
}
